package com.ezlearning.platform.controller;

import com.ezlearning.platform.dto.StudentDto;
import com.ezlearning.platform.model.Student;
import com.ezlearning.platform.repositories.StudentRepository;
import com.ezlearning.platform.services.core.impl.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/students")
public class StudentController {

    private StudentService studentService;
    private StudentRepository studentRepository;

    @Autowired
    public StudentController(StudentService studentService, StudentRepository studentRepository) {
        this.studentService = studentService;
        this.studentRepository = studentRepository;
    }

    @GetMapping("/add")
    public String addStudent(Model model) {
        model.addAttribute("student", new StudentDto());
        return "students/student-add";
    }

    @PostMapping("/save")
    public String saveStudent(StudentDto student) {
        studentService.create(student);

        return "redirect:/students";
    }

    @GetMapping("/edit/{id_student}")
    public String getStudentForUpdate(@PathVariable Long id_student,
                                      Model model) {
        try {
            Student studentActual = studentRepository.findById(id_student).get();
            model.addAttribute("student", studentActual);
            return "students/student-edit";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @PostMapping("/update/{id_student}")
    public String updateStudent(@PathVariable Long id_student,
                                Student student, RedirectAttributes attributes, Model model){

        try {
            Student currentStudent = studentRepository.findById(id_student).get();
            currentStudent.setNomStudent(student.getNomStudent());
            currentStudent.setApeStudent(student.getApeStudent());
            currentStudent.setCorreoStudent(student.getCorreoStudent());
            currentStudent.setDescStudent(student.getDescStudent());
            currentStudent.setImgurl(student.getImgurl());

            studentService.update(student);
            attributes.addAttribute("id_student", id_student);

            return "redirect:/students/{id_student}";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @PostMapping("/patch/{id_student}")
    public String patchStudent(@PathVariable Long id_student, Student student, RedirectAttributes attributes, Model model) {

        try {
            Student current = studentRepository.findById(id_student).get();
            current.setDetalleStudent(student.getDetalleStudent());
            studentService.patch(current);

            attributes.addAttribute("id_student", id_student);
            return "redirect:/students/{id_student}";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping
    public String getStudentsList(Model model) {
        List<Student> students = studentService.getAll();
        model.addAttribute("students", students);
        return "students/students";
    }

    @GetMapping("/delete/{id_student}")
    public String deleteStudent(@PathVariable Long id_student, Model model) {
        try {
            Student studentActual = studentRepository.findById(id_student).get();
            studentService.delete(studentActual);

            return "redirect:/students";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping("/{id_student}")
    public String getStudentrDetail(@PathVariable Long id_student, Model model) {
        try {
            Student student = studentRepository.findById(id_student).get();
            model.addAttribute("student", student);
            return "students/student-detail";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping("/search")
    public String getStudentsList(@RequestParam(required = false) String query, Model model) {
        List<Student> students = studentService.search(query);
        model.addAttribute("students", students);
        return "students/students";
    }

}
